﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Coffee_Restaurant.Database;

namespace Coffee_Restaurant.DAL
{
    class AppSettingDa
    {

        public static string GetValue(string key)
        {

            using (var db = new DatabaseEntities())
            {
                if (!string.IsNullOrEmpty(key))
                {
                    var model = db.AppSettings.FirstOrDefault(x => x.Key == key);
                    if (model != null)
                    {
                        return model.Value;
                    }
                }
                return null;
            }
        }

        public static int Update(string key, string value)
        {
            try
            {
                using (var db = new DatabaseEntities())
                {
                    if (!string.IsNullOrEmpty(key))
                    {
                        var model = db.AppSettings.FirstOrDefault(x => x.Key == key);
                        if (model != null)
                        {
                            model.Value = value;
                        }
                        else
                        {
                            var appsetting = new AppSetting();
                            appsetting.Key = key;
                            appsetting.Value = value;
                            db.AppSettings.Add(appsetting);
                        }
                    }
                    return db.SaveChanges();
                }
            }
            catch (Exception)
            {

                return 0;
            }
        }
    }
}
